/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.liu.modules.business.service.impl;

import com.liu.modules.business.domain.EjlAcessNovelLog;
import com.liu.modules.business.domain.NovelInfo;
import com.liu.modules.business.repository.NovelInfoRepository;
import com.liu.utils.*;
import lombok.RequiredArgsConstructor;
import com.liu.modules.business.repository.EjlAcessNovelLogRepository;
import com.liu.modules.business.service.EjlAcessNovelLogService;
import com.liu.modules.business.service.dto.EjlAcessNovelLogDto;
import com.liu.modules.business.service.dto.EjlAcessNovelLogQueryCriteria;
import com.liu.modules.business.service.mapstruct.EjlAcessNovelLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Liu
* @date 2022-07-08
**/
@Service
@RequiredArgsConstructor
public class EjlAcessNovelLogServiceImpl implements EjlAcessNovelLogService {

    private final EjlAcessNovelLogRepository ejlAcessNovelLogRepository;
    private final EjlAcessNovelLogMapper ejlAcessNovelLogMapper;
    private final NovelInfoRepository novelInfoRepository;

    @Override
    public Map<String,Object> queryAll(EjlAcessNovelLogQueryCriteria criteria, Pageable pageable){
        Page<EjlAcessNovelLog> page = ejlAcessNovelLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ejlAcessNovelLogMapper::toDto));
    }

    @Override
    public List<EjlAcessNovelLogDto> queryAll(EjlAcessNovelLogQueryCriteria criteria){
        return ejlAcessNovelLogMapper.toDto(ejlAcessNovelLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public EjlAcessNovelLogDto findById(String logId) {
        EjlAcessNovelLog ejlAcessNovelLog = ejlAcessNovelLogRepository.findById(logId).orElseGet(EjlAcessNovelLog::new);
        ValidationUtil.isNull(ejlAcessNovelLog.getLogId(),"EjlAcessNovelLog","logId",logId);
        return ejlAcessNovelLogMapper.toDto(ejlAcessNovelLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EjlAcessNovelLogDto create(EjlAcessNovelLog resources) {
        resources.setLogId(IdUtil.simpleUUID()); 
        return ejlAcessNovelLogMapper.toDto(ejlAcessNovelLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(EjlAcessNovelLog resources) {
        EjlAcessNovelLog ejlAcessNovelLog = ejlAcessNovelLogRepository.findById(resources.getLogId()).orElseGet(EjlAcessNovelLog::new);
        ValidationUtil.isNull( ejlAcessNovelLog.getLogId(),"EjlAcessNovelLog","id",resources.getLogId());
        ejlAcessNovelLog.copy(resources);
        ejlAcessNovelLogRepository.save(ejlAcessNovelLog);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String logId : ids) {
            ejlAcessNovelLogRepository.deleteById(logId);
        }
    }

    @Override
    public void download(List<EjlAcessNovelLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EjlAcessNovelLogDto ejlAcessNovelLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", ejlAcessNovelLog.getUserId());
            map.put("小说ID", ejlAcessNovelLog.getNovelId());
            map.put("章节索引", ejlAcessNovelLog.getChapterIndex());
            map.put("章节名称", ejlAcessNovelLog.getChapterTitle());
            map.put("小说名称", ejlAcessNovelLog.getNovelName());
            map.put("操作时间", ejlAcessNovelLog.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<NovelInfo> queryRecommend(EjlAcessNovelLogQueryCriteria criteria, Pageable pageable) {
        List<Map<String, Object>> list = ejlAcessNovelLogRepository.queryRecommend(pageable.getPageNumber(), pageable.getPageSize());
        List novelInfoList =new ArrayList();
        for (Map<String, Object> map : list) {
            String novel_id = (String)map.get("novel_id");
            NovelInfo novelInfo = novelInfoRepository.findById(novel_id).orElse(null);
            if(novelInfo != null){
                novelInfoList.add(novelInfo);
            }
        }
        return novelInfoList;
    }
}